import axios from 'axios'
import { Modal } from 'view-design'
import jscookie from 'js-cookie'

axios.defaults.baseURL="/api"
axios.create({
    validateStatus:function (status) {
        return status < 400;
    }
})
axios.interceptors.request.use(config=>{
    let time = new Date();
    config.headers.AuthorizationNotes = time.getTime()
    let token = sessionStorage.getItem("token")
    if(token!=null){
        jscookie.set("token",token)
    }
    return config
})

axios.interceptors.response.use(res=>{
    let token = sessionStorage.getItem("token")
    if(token!=null) {
        jscookie.set("token", "")
    }
    return res;
},error=>{
    switch (error.response.status) {
        case 403:
            Modal.error({
                title:"警告",
                content:"嘿嘿，你还权限哦"
            })
            break;
        case 500:
            Modal.error({
                title:"警告",
                content:"服务器出错了"
            })
            break;
        case 404:
            Modal.error({
                title:"警告",
                content:"你的资源飞走了"
            })
            break;
        case 401:
            window.sessionStorage.removeItem("token")
            Modal.error({
                title:"警告",
                content:"认证已过期，请重新验证",
                onOk(){
                    window.location.reload();
                }
            })
            break;
        default :
            Modal.error({
                title:"警告",
                content:"不知道啥错"
            })
    }
})

export default {
    get(url, params) {
        return new Promise((resolve, reject) => {
            axios.get("/backstage"+url, params)
                .then(res => {
                    resolve(res);
                })
                .catch(err => {
                    reject(err)
                })
        });
    },
    post(url,params){
        return new Promise((resolve, reject) => {
            axios.post("/backstage"+url, params)
                .then(res => {
                    resolve(res);
                })
                .catch(err => {
                    reject(err)
                })
        });
    },
    login(url,params){
        return new Promise((resolve, reject) => {
            axios.post("/login"+url, params)
                .then(res => {
                    resolve(res);
                })
                .catch(err => {
                    reject(err)
                })
        });
    },
    delete(url, params){
        return new Promise((resolve, reject) =>{
            axios.delete("/backstage"+url, params)
                .then(res=>{
                    resolve(res)
                }).catch(err=>{
                    reject(err)
            })
        })
    },
    put(url, params){
        return new Promise((resolve, reject) =>{
            axios.put("/backstage"+url, params)
                .then(res=>{
                    resolve(res)
                }).catch(err=>{
                reject(err)
            })
        })
    },
}
